package cn.wolfcode.p2p.web.controller;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UploadUtil;
import cn.wolfcode.p2p.web.util.RequiredLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 前台:实名认证页面
 */
@Controller
public class RealAuthController {
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IUserInfoService userInfoService;

    // 进入实名认证申请页面/待审核页面/审核通过页面
    @RequestMapping("/realAuth")
    @RequiredLogin
    public String toApplyPage(Model model) {
        // 1.获取当前登录信息,判断当前是否已经认证通过
        UserInfo current = userInfoService.getCurrent();
        if (!current.getHasRealAuth()) {
            // 2.没有通过实名认证
            if (current.getRealAuthId() == null) {
                return "realAuth";
            } else {
                model.addAttribute("auditing", true);
                return  "realAuth_result";
            }
        } else {
            // 3.已经实名认证通过
            model.addAttribute("auditing", false);
            model.addAttribute("realAuth", realAuthService.get(current.getRealAuthId()));
            return "realAuth_result";
        }
    }

    // 提交实名认证申请
    @PostMapping("/realAuth_save")
    @ResponseBody
    @RequiredLogin
    public JSONResult apply(RealAuth vo){
        realAuthService.apply(vo);
        return new JSONResult();
    }

    // 文件上传
    @RequestMapping("/uploadImage")
    @ResponseBody
    public String uploadImage(MultipartFile pic){
        return UploadUtil.upload(pic);
    }

}
